﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;
using System.Text;

namespace Nivdal
{
    public partial class NewsPaper : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.NewsPaper_PageTitle;
            OnlineUser ou = (OnlineUser)Session["User"];
            string country = ou.Country;
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            string UserNewsPaperPriv;
            string NewspaperId;
            
            if (Request.QueryString["Id"] != null)
                NewspaperId = Request.QueryString["Id"];
            else
                NewspaperId = ou.Newspaper;

            if ((NewspaperId == null) || NewspaperId == "Null")
                Response.Redirect("~/AllNewspapers.aspx");

            NewsPaperIdHiddenfield.Value = NewspaperId;

            NewStoriesPanel.Visible = GND.ReadNivdalBoolData("UserId", "NewsPaperId", "Nivdal_IsUserSubscribedToNewspaper", ou.UserId, NewspaperId);
            UserNewsPaperPriv = GND.ReadNivdalIntData("UserId", "NewsPapaerId", "Nivdal_GetNewsPaperPrivsLevel", ou.UserId, NewspaperId);
            NewspaperNameTitleLabel.Text = GND.ReadNivdalStringData("NewspaperId", "Nivdal_GetNewsPaperNameById", NewspaperId);

            //ShowPopUpMsg("UserNewsPaperPriv is: " + UserNewsPaperPriv);
            if (UserNewsPaperPriv == "2" || UserNewsPaperPriv == "3")
                AddStoryPanel.Visible = true;
            if (UserNewsPaperPriv == "3")
                AdminPanel.Visible = true;

            OuterAdminPanel.Visible = (AddStoryPanel.Visible || AdminPanel.Visible);
            
            

            if (GND.ReadNivdalBoolData("NewsPaperId", "Nivdal_GetNewspaperDir", NewspaperId))
                NewspaperDirPanel.Direction = ContentDirection.RightToLeft;
            else
                NewspaperDirPanel.Direction = ContentDirection.LeftToRight;


            if (!IsPostBack)
            {
                RubricsDataSource.SelectParameters.Add("NewsPaperId", NewspaperId);
                NewsPaperTopStoriesDataSource.SelectParameters.Add("NewsPaperId", NewspaperId);
                if (ou.UserId != null)
                    NewsPaperTopStoriesDataSource.SelectParameters.Add("UserId", ou.UserId);
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    ApprovalPendingDataSource.SelectParameters.Add("NewsPaperId", NewspaperId);
                    ApprovalPendingDataSource.SelectParameters.Add("UserId", ou.UserId);
                    OtherNewspapersDataSource.SelectParameters.Add("NewsPaperId", NewspaperId);
                }
                else
                {
                    StoriesPendingApproval.Visible = false;
                }
            }

        }

        protected void RedirectToOtherNewspaper(object sender, EventArgs e)
        {
            string NewspeperId = OtherNewspapersDDL.SelectedValue;
            Response.Redirect("~/NewsPaper.aspx?Id=" + NewspeperId);
        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }

        private void ShowPopUpMsg(string msg)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("alert('");
            sb.Append(msg.Replace("\n", "\\n").Replace("\r", "").Replace("'", "\\'"));
            sb.Append("');");
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "showalert", sb.ToString(), true);
        }

        protected void MarkAllAsRead(object sender, EventArgs e)
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            GND.ExecNivdalSQL("UserId", "NewspaperId", "Nivdal_MarkAllNewspaperStoriesAsRead", ou.UserId, NewsPaperIdHiddenfield.Value);
            if (NewsPaperTopStoriesDataSource.SelectParameters.Count == 3)
            {
                Parameter p = NewsPaperTopStoriesDataSource.SelectParameters[2];
                NewsPaperTopStoriesDataSource.SelectParameters.Remove(p);
            }
            StoriesRepeater.DataBind();
        }

        protected void ShowOnlyNew(object sender, EventArgs e)
        {
            NewsPaperTopStoriesDataSource.SelectParameters.Add("NewOnly", "1");
            ShowOnlyNewButton.Visible = false;
            RubricsRepeater.DataBind();
        }
     
    }


}